package com.lkd.handler;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lkd.business.MsgHandler;
import com.lkd.config.TopicConfig;
import com.lkd.contract.SupplyContract;
import com.lkd.emq.Topic;
import com.lkd.entity.ChannelEntity;
import com.lkd.entity.VendingMachineEntity;
import com.lkd.service.ChannelService;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author : Bing
 * @Date 2023/3/2 18:52
 */
@Component
@Topic(TopicConfig.VMS_SUPPLY_TOPIC)
public class CompleteSupplyHandler implements MsgHandler {

    @Autowired
    private ChannelService channelService;

    @Autowired
    private VendingMachineService vendingMachineService;

    /**
     * 运营工单修改售货机状态
     *
     * @param jsonMsg EMQ消息
     * @throws IOException
     */
    @Override
    public void process(String jsonMsg) throws IOException {

        //解析消息
        SupplyContract contract = JsonUtil.getByJson(jsonMsg, SupplyContract.class);

        //更改货道信息
        List<ChannelEntity> channeles = channelService.getChannelesByInnerCode(contract.getInnerCode());

        Map<String, ChannelEntity> channelEntityMap = channeles.stream().collect(Collectors.toMap(ChannelEntity::getChannelCode, e -> e));

        List<ChannelEntity> collect = contract.getSupplyData().stream().map(each -> {
            ChannelEntity channelEntity = new ChannelEntity();
            if (channelEntityMap.containsKey(each.getChannelId())) {
                channelEntity = channelEntityMap.get(each.getChannelId());
                channelEntity.setLastSupplyTime(LocalDateTime.now());
                channelEntity.setCurrentCapacity(each.getCapacity() + channelEntity.getCurrentCapacity());
            }
            return channelEntity;
        }).collect(Collectors.toList());

        channelService.saveOrUpdateBatch(collect);

//        channeles.forEach(each -> {
//            if (supplyChannelMap.get(each.getChannelCode()) != null){
//
//                SupplyChannel supplyChannel = supplyChannelMap.get(each.getChannelCode());
//
//                each.setLastSupplyTime(LocalDateTime.now());
//                each.setCurrentCapacity(each.getCurrentCapacity() + supplyChannel.getCapacity());
//
//                channelService.updateById(each);
//            }
//        });

//        for (ChannelEntity channele : channeles) {
//            if (supplyChannelMap.get(channele.getChannelCode()) != null){
//
//                SupplyChannel supplyChannel = supplyChannelMap.get(channele.getChannelCode());
//
//                channele.setLastSupplyTime(LocalDateTime.now());
//                channele.setCurrentCapacity(channele.getCurrentCapacity()+supplyChannel.getCapacity());
//
//                channelService.updateById(channele);
//            }
//        }

//        for (SupplyChannel supplyDatum : contract.getSupplyData()) {
//            for (ChannelEntity channele : channeles) {
//                if (supplyDatum.getChannelId().equals(channele.getChannelCode())) {
//
//                    Integer currentCapacity = channele.getCurrentCapacity() + supplyDatum.getCapacity();
//
//                    channele.setCurrentCapacity(currentCapacity);
//                    channele.setLastSupplyTime(LocalDateTime.now());
//
//                    channelService.updateById(channele);
//                }
//            }
//        }

//        for (ChannelEntity channele : channeles) {
//            if (channele.getSkuId()!=0){
//                for (SupplyChannel supplyDatum : contract.getSupplyData()) {
//
//                    Integer currentCapacity = channele.getCurrentCapacity()+supplyDatum.getCapacity();
//
//                    LambdaUpdateWrapper<ChannelEntity> wrapper = new LambdaUpdateWrapper<>();
//                    wrapper.eq(ChannelEntity::getChannelCode,supplyDatum.getChannelId())
//                            .eq(ChannelEntity::getInnerCode,contract.getInnerCode())
//                            .set(ChannelEntity::getCurrentCapacity,currentCapacity)
//                            .set(ChannelEntity::getLastSupplyTime, LocalDateTime.now());
//
//                    channelService.update(null,wrapper);
//                }
//            }
//        }


        //更改售货机上次补货时间
        UpdateWrapper<VendingMachineEntity> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(VendingMachineEntity::getInnerCode, contract.getInnerCode())
                .set(VendingMachineEntity::getLastSupplyTime, new Date());
        vendingMachineService.update(wrapper);


    }

}
